package com.dbgs.insurance.insuredperson.fragments;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v7.widget.AppCompatTextView;
import android.text.format.DateUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;

import com.dbgs.insurance.insuredperson.R;
import com.dbgs.insurance.insuredperson.adapter.NewsInfoAdapter;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshListView;
import com.hss.base.BaseFragment;
import com.lz.commonlibrary.utils.NetManagerUtil;
import com.meetme.android.multistateview.MultiStateView;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;

/**
 * Created by Administrator on 2018/5/14.
 */

public class NewsFragment extends BaseFragment {


    @BindView(R.id.tv_news)
    AppCompatTextView tvNews;
    @BindView(R.id.refreshListView)
    PullToRefreshListView refreshListView;
    @BindView(R.id.multiStateView_content)
    MultiStateView multiStateViewContent;
    Unbinder unbinder;
    private View root_view;

    NewsInfoAdapter mNewsInfoAdapter;
    private int page = 1;
    int isDrop = 0;// 0 first 1 refresh 2 more

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        if (root_view == null) {
            root_view = inflater.inflate(R.layout.fragment_news, container,
                    false);
        }
        ViewGroup parent = (ViewGroup) root_view.getParent();
        if (parent != null) {
            parent.removeView(root_view);
        }

        unbinder = ButterKnife.bind(this, root_view);
        return root_view;
    }

    @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onViewCreated(view, savedInstanceState);
        initViews();
        initListener();
    }

    @Override
    public void initViews() {
        super.initViews();
        mNewsInfoAdapter = new NewsInfoAdapter(getActivity());
        refreshListView.getRefreshableView().setAdapter(mNewsInfoAdapter);

        // **************空列表**************

        View emptyLoadingView = LayoutInflater.from(getActivity()).inflate(
                R.layout.view_empty, null);
        emptyLoadingView.setLayoutParams(new ViewGroup.LayoutParams(
                ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
        refreshListView.getRefreshableView().setEmptyView(emptyLoadingView);
    }

    @Override
    public void initListener() {
        super.initListener();
        refreshListView.setOnRefreshListener(refreshListener);
    }

    PullToRefreshBase.OnRefreshListener2<ListView> refreshListener = new PullToRefreshBase.OnRefreshListener2<ListView>() {
        @Override
        public void onPullDownToRefresh(PullToRefreshBase<ListView> refreshView) {

            String label = DateUtils.formatDateTime(getActivity().getApplicationContext(), System.currentTimeMillis(),
                    DateUtils.FORMAT_SHOW_TIME | DateUtils.FORMAT_SHOW_DATE
                            | DateUtils.FORMAT_ABBREV_ALL);
            refreshView.getLoadingLayoutProxy().setLastUpdatedLabel(label);
            if (NetManagerUtil.isConnectNet(getActivity())) {
                isDrop = 1;// 设置操作
                // newsAdapter.clear();
                if (refreshListView.getMode() != PullToRefreshBase.Mode.PULL_FROM_START) {
                    refreshListView.setMode(PullToRefreshBase.Mode.PULL_FROM_START);
                }
                loadData();
            } else {
                //refreshListView.onRefreshComplete();
                handler.sendEmptyMessage(100);
            }

        }

        @Override
        public void onPullUpToRefresh(PullToRefreshBase<ListView> refreshView) {
            String label = DateUtils.formatDateTime(getActivity()
                            .getApplicationContext(), System.currentTimeMillis(),
                    DateUtils.FORMAT_SHOW_TIME | DateUtils.FORMAT_SHOW_DATE
                            | DateUtils.FORMAT_ABBREV_ALL);
            // Update the LastUpdatedLabel
            refreshView.getLoadingLayoutProxy().setLastUpdatedLabel(label);
            if (NetManagerUtil.isConnectNet(getActivity())) {
                isDrop = 2;// 设置操作
                loadData();
            } else {
                // ToastUtil.showToast(MallActivity.this, R.string.check_net,
                // false);
                refreshListView.onRefreshComplete();

            }

        }
    };

    Handler handler = new Handler(){
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);

            refreshListView.onRefreshComplete();
            showNetErr();
        }
    };


    private void loadData() {
        switch (isDrop) {
            case 0:
                multiStateViewContent.setState(MultiStateView.ContentState.LOADING);
                page = 1;
                break;
            case 1:
                page = 1;
                break;
            case 2:
                ++page;
                break;
            default:
                break;
        }
        getHealthInfo();
    }

    private void showNetErr() {
        multiStateViewContent.setState(MultiStateView.ContentState.ERROR_NETWORK);
        View view = multiStateViewContent.findViewById(R.id.ll_net_error);
        view.setOnClickListener(netErrClick);
    }

    View.OnClickListener netErrClick = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if (NetManagerUtil.isConnectNet(getActivity())) {
                isDrop = 1;
                loadData();
            }
        }
    };

    private void getHealthInfo(){

    }

    @Override
    public void showMSVLoading() {

    }

    @Override
    public void showMSVEmpty() {

    }

    @Override
    public void showMSVNetErr() {

    }

    @Override
    public void showMSVUnknowErr() {

    }

    @Override
    public void showMSVContent() {

    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }
}
